package com.qf.dao.impl;

import com.qf.dao.UserDao;
import com.qf.model.User;
import com.qf.model.UserCard;
import com.qf.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements UserDao {

    private QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
    @Override
    public int regist(User user) {
        String sql = "insert into user (uid,phone,password) values(null,?,?)";
        try {
            int update = queryRunner.update(sql, user.getPhone(), user.getPassword());
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public User login(User user) {
        String sql = "select * from user where phone=? and password=?";
        try {
            User query = queryRunner.query(sql, new BeanHandler<>(User.class),user.getPhone(),user.getPassword());
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public User findUser(String phone) {
        String sql = "select * from user where phone=?";
        try {
            User query = queryRunner.query(sql, new BeanHandler<>(User.class),phone);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int update(User user1) {
        String sql = "update user set name = ?, idnumber = ? where uid = ?";
        try {
            int update = queryRunner.update(sql, user1.getName(), user1.getIdnumber(),user1.getUid());
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int setPayPassword(String payPassword,int uid,String cid) {
        String sql = "insert into usercard values(?,?,?,?,?,?)";

        try {
            int update = queryRunner.update(sql, cid, "艾绒银行", 0, payPassword, "Ⅰ", uid);
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public UserCard findOnCard(String cardnum,String onCardPassword) {
        String sql = "select * from oncard where cid=? and password = ?";
        try {
            UserCard onCard = queryRunner.query(sql, new BeanHandler<>(UserCard.class),cardnum,onCardPassword);
            return onCard;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int addUserCard(UserCard onCard) {
        String sql = "insert into usercard values(?,?,?,?,?,?)";
        try {
            int update = queryRunner.update(sql, onCard.getCid(), onCard.getBankname(), onCard.getMoney(), onCard.getPassword(), onCard.getCardtype(), onCard.getUid());
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    //根据name查询user对象
    @Override
    public User select(User user) {
        String sql = "select * from user where uid=?";
        try {
            User select = queryRunner.query(sql,new BeanHandler<>(User.class),user.getUid());
            return select;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //修改我的信息
    @Override
    public int updateUserMsg(User user) {
        String  sql = "update user set  name=?,sex=?,country=?,idtype=?,idnumber=?,phone=?,address=? where uid=?";
        try {
            int row = queryRunner.update(sql,user.getName(),user.getSex(),user.getCountry(),user.getIdtype(),user.getIdnumber(),user.getPhone(),user.getAddress(),user.getUid());
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<UserCard> findAllCard(User user1) {

        String sql = "select * from usercard where uid = ?";
        try {
            List<UserCard> query = queryRunner.query(sql, new BeanListHandler<>(UserCard.class), user1.getUid());
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
